package net.bw.realtime.tmall.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.bw.realtime.tmall.bean.*;
import net.bw.realtime.tmall.service.*;
import net.bw.realtime.tmall.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/*
 * @ Author：liuyawei
 * @ Date：2025-05-28
 */
@RestController
@RequestMapping("/tmall/sale")
public class SaleController {

    private static final String DATE_DEFAULT_VALUE = "2024-11-10";

    // 统计销售量
    @Autowired
    SaleCountStatsService saleCountStatsService;
    @RequestMapping("/saleCount")
    public JSONObject getSaleCount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date){

        // 1.获取参数
        if(DATE_DEFAULT_VALUE.equals(date)){
            date = DateUtils.nowDate();
        }

        // 2.调用service
        Long saleCount = saleCountStatsService.selectSaleCount(date);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", 0);
        jsonObject.put("msg", "");
        jsonObject.put("data", saleCount);

        // 4.返回数据
        return jsonObject;
    }

    // 统计销售额
    @Autowired
    SaleAmountStatsService saleAmountStatsService;
    @RequestMapping("/saleAmount")
    public JSONObject getSaleAmount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date){

        // 1.获取参数
        if(DATE_DEFAULT_VALUE.equals(date)){
            date = DateUtils.nowDate();
        }

        // 2.调用service
        Double saleAmount = saleAmountStatsService.selectSaleAmount(date);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", 0);
        jsonObject.put("msg", "");
        jsonObject.put("data", saleAmount);

        // 4.返回数据
        return jsonObject;


    }

    // 省份分布统计
    @Autowired
    SaleProvinceStatsService saleProvinceStatsService;
    @RequestMapping("/province")
    public JSONObject getProvince(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date){

        // 1.获取参数
        if(DATE_DEFAULT_VALUE.equals(date)){
            date = DateUtils.nowDate();
        }

        // 2.调用service
        List<SaleProvinceReport> provinceReports = saleProvinceStatsService.selectSaleAmount(date);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", 0);
        jsonObject.put("msg", "");

        JSONObject object = new JSONObject();
        jsonObject.put("data", object);

        JSONArray array1 = new JSONArray();
        object.put("mapData", array1);

        for (SaleProvinceReport provinceReport : provinceReports) {
            JSONObject object1 = new JSONObject();
            object1.put("name", provinceReport.getProvince());
            object1.put("value", provinceReport.getSale_amount());
            array1.add(object1);
        }

        // 4.返回数据
        return jsonObject;

    }

    // 商品统计
    @Autowired
    SaleSkuStatsService saleSkuStatsService;
    @RequestMapping("/sku")
    public JSONObject getSku(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date){

        //  1.获取参数
        if(DATE_DEFAULT_VALUE.equals(date)){
            date = DateUtils.nowDate();
        }

        //  2.调用service
        List<SaleSkuReport> saleSkuReports = saleSkuStatsService.selectSaleAmount(date);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", 0);
        jsonObject.put("msg", "");

        JSONObject object = new JSONObject();
        jsonObject.put("data", object);

        JSONArray columns = new JSONArray();
        JSONArray rows = new JSONArray();
        object.put("columns", columns);
        object.put("rows", rows);

        JSONObject c1 = new JSONObject();
        c1.put("id", "sku_name");
        c1.put("name", "商品名称");
        c1.put("textAlign", "center");
        columns.add(c1);

        JSONObject c2 = new JSONObject();
        c2.put("id", "sale_amount");
        c2.put("name", "销售金额");
        c2.put("textAlign", "center");
        columns.add(c2);

        for (SaleSkuReport saleSkuReport : saleSkuReports) {
            JSONObject r1 = new JSONObject();
            r1.put("sku_name", saleSkuReport.getSku_name());
            r1.put("sale_amount", saleSkuReport.getSale_amount());
            rows.add(r1);
        }

        //  4.返回数据
        return jsonObject;

    }

    // 商品总数量
    @RequestMapping("/skuCount")
    public JSONObject getselectSkuCount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date){

        // 1.获取参数
        if(DATE_DEFAULT_VALUE.equals(date)){
            date = DateUtils.nowDate();
        }

        // 2.调用service
        Double selectSkuCount = saleSkuStatsService.selectSkuCount(date);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        jsonObject.put("data",selectSkuCount);

        // 4.返回数据
        return jsonObject;

    }

    // 设备统计
    @Autowired
    SaleDeviceStatService saleDeviceStatsService;
    @RequestMapping("/device")
    public JSONObject getDevice(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date){

        //  1.获取参数
        if(DATE_DEFAULT_VALUE.equals(date)){
            date = DateUtils.nowDate();
        }

        //  2.调用service
        List<SaleDeviceReport> saleDeviceReports = saleDeviceStatsService.selectSaleAmount(date);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", 0);
        jsonObject.put("msg", "");

        JSONArray array = new JSONArray();
        jsonObject.put("data", array);

        for (SaleDeviceReport saleDeviceReport : saleDeviceReports) {
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("device_type", saleDeviceReport.getDevice_type());
            jsonObject1.put("sale_amount", saleDeviceReport.getSale_amount());
            array.add(jsonObject1);
        }

        //  4.返回数据
        return jsonObject;
    }

    // 小时统计设备
    @Autowired
    SaleDeviceHourStatsService saleHourDeviceStatsService;
    @RequestMapping("/deviceHour")
    public JSONObject getDeviceHour(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date){

        // 1.获取参数
        if(DATE_DEFAULT_VALUE.equals(date)){
            date = DateUtils.nowDate();
        }

        // 2.调用service
        List<SaleDeviceHourReport> saleDeviceHourReports = saleHourDeviceStatsService.selectSaleAmount(date);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject jsonObject1 = new JSONObject();
        jsonObject.put("data",jsonObject1);

        JSONArray array = new JSONArray();
        JSONArray array1 = new JSONArray();

        JSONObject jsonObject2 = new JSONObject();
        JSONObject jsonObject3 = new JSONObject();

        jsonObject2.put("name","PC");
        jsonObject3.put("name","无线");

        JSONArray array2 = new JSONArray();
        JSONArray array3 = new JSONArray();
        for (SaleDeviceHourReport saleDeviceHourReport : saleDeviceHourReports) {
            array.add(saleDeviceHourReport.getHour());
            array2.add(saleDeviceHourReport.getPc_total_amount());
            array3.add(saleDeviceHourReport.getNo_total_amount());
        }
        jsonObject1.put("categories",array);


        jsonObject2.put("data",array2);
        jsonObject3.put("data",array3);

        array1.add(jsonObject2);
        array1.add(jsonObject3);

        jsonObject1.put("series",array1);

        return jsonObject;
    }

    // 统计城市TopN
    @Autowired
    SaleTopCityStatsService saleTopCityStatsService;
    @RequestMapping("/topCity")
    public JSONObject getTopCity(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date){

        // 1.获取参数
        if(DATE_DEFAULT_VALUE.equals(date)){
            date = DateUtils.nowDate();
        }

        // 2.调用service
        List<SaleTopCityReport> saleTopCityReports = saleTopCityStatsService.selectSaleAmount(date);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", 0);
        jsonObject.put("msg", "");

        JSONObject object = new JSONObject();
        jsonObject.put("data", object);

        JSONArray columns = new JSONArray();
        JSONArray rows = new JSONArray();
        object.put("columns", columns);
        object.put("rows", rows);

        JSONObject c1 = new JSONObject();
        c1.put("id", "city");
        c1.put("name", "城市名称");
        c1.put("textAlign", "center");
        columns.add(c1);

        JSONObject c2 = new JSONObject();
        c2.put("id", "sale_amount");
        c2.put("name", "销售金额");
        c2.put("textAlign", "center");
        columns.add(c2);

        JSONObject c3 = new JSONObject();
        c3.put("id", "rank");
        c3.put("name", "排名");
        c3.put("textAlign", "center");
        columns.add(c3);

        for (SaleTopCityReport saleTopCityReport : saleTopCityReports) {
            JSONObject r1 = new JSONObject();
            r1.put("city", saleTopCityReport.getCity());
            r1.put("sale_amount", saleTopCityReport.getSale_amount());
            r1.put("rank", saleTopCityReport.getRank());
            rows.add(r1);
        }

        //  4.返回数据
        return jsonObject;
    }

    // 统计省份TopN
    @Autowired
    SaleTopProvinceStatsService saleTopProvinceStatsService;
    @RequestMapping("/topProvince")
    public JSONObject getTopProvince(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date){

        // 1.获取参数
        if(DATE_DEFAULT_VALUE.equals(date)){
            date = DateUtils.nowDate();
        }

        // 2.调用service
        List<SaleTopProvinceReport> saleTopProvinceReports = saleTopProvinceStatsService.selectSaleAmount(date);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", 0);
        jsonObject.put("msg", "");

        JSONObject object = new JSONObject();
        jsonObject.put("data", object);

        JSONArray columns = new JSONArray();
        JSONArray rows = new JSONArray();
        object.put("columns", columns);
        object.put("rows", rows);

        JSONObject c1 = new JSONObject();
        c1.put("id", "province");
        c1.put("name", "省份名称");
        c1.put("textAlign", "center");
        columns.add(c1);

        JSONObject c2 = new JSONObject();
        c2.put("id", "sale_amount");
        c2.put("name", "销售金额");
        c2.put("textAlign", "center");
        columns.add(c2);

        JSONObject c3 = new JSONObject();
        c3.put("id", "rank");
        c3.put("name", "排名");
        c3.put("textAlign", "center");
        columns.add(c3);

        for (SaleTopProvinceReport saleTopProvinceReport : saleTopProvinceReports) {
            JSONObject r1 = new JSONObject();
            r1.put("province", saleTopProvinceReport.getProvince());
            r1.put("sale_amount", saleTopProvinceReport.getSale_amount());
            r1.put("rank", saleTopProvinceReport.getRank());
            rows.add(r1);
        }

        //  4.返回数据
        return jsonObject;

    }

    // 获取省份平均金额
    @RequestMapping("/provinceAmount")
    public JSONObject getProvinceAmount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date){

        // 1.获取参数
        if(DATE_DEFAULT_VALUE.equals(date)){
            date = DateUtils.nowDate();
        }

        // 2.调用service
        Double provinceAmount = saleProvinceStatsService.selectProvinceAmount(date);

        // 3.判断是否有数据
        if(null == provinceAmount){
            return null;
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        jsonObject.put("data",provinceAmount);

        // 4.返回数据
        return jsonObject;

    }

    // 获取省份平均下单量
    @RequestMapping("/provinceOrderCount")
    public JSONObject getProvinceOrderCnt (@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE)String date){

        // 1.获取参数
        if(DATE_DEFAULT_VALUE.equals(date)){
            date = DateUtils.nowDate();
        }

        // 2.调用service
        Double provinceOrderCnt = saleProvinceStatsService.selectProvinceOrderCnt(date);

        // 3.判断是否有数据
        if(null == provinceOrderCnt){
            return null;
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        jsonObject.put("data",provinceOrderCnt);

        // 4.返回数据
        return jsonObject;

    }

}
